package org.demo.web;

import org.demo.vo.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RestController("")
public class TestController {

    private static Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/demo/redis")
    public ApiResult<String> testRedis(HttpServletRequest request) {
        ApiResult<String> result = new ApiResult<>();
        ValueOperations<String, String> vo = stringRedisTemplate.opsForValue();
        vo.set("tmp", "hello redis");
        stringRedisTemplate.expire("tmp", 60, TimeUnit.SECONDS);
        String t = vo.get("tmp");
        result.setData(t);
        logger.error("测试成功");
        return result;
    }
}
